home *** CD-ROM | disk | FTP | other *** search
- #ifndef __VECTOR__
- #define __VECTOR__ 1
-
- #pragma once
-
- #ifndef __DATAAREA__
- #include "DataArea.h"
- #endif
-
- #ifndef __TYPES__
- #include <types.h>
- #endif
-
-
- /*µ class Vector
- ** A vector is a collection of pointers to items. The vector is zero-based.
- */
- class Vector : private DataArea {
- public:
- Vector();
-
- int IVector();
- int IVector(size_t initialSize, size_t increment);
- int IVector(const Vector *aVector);
- // Initialize the vector
-
-
- void SetIncrement(size_t anIncrement);
- int Length() const;
- void *At(int i) const;
-
- Boolean AddItem(const void *anItem);
- Boolean AddOnce(const void *anItem);
- Boolean AddOnce(const Vector *aVector);
- // Add the item to the Vector. Return true if the item is now in the
- // Vector, false if it isn't. The AddOnce method adds the item to the
- // Vector only if it isn't already in the Vector. Comparison is done by
- // pointer equality. The AddOnce method given a reference to a Vector
- // adds unique items from that Vector to this Vector.
-
- void AtPut(int i, const void *anItem);
- // Set the i'th item to be anItem.
-
- int IndexOf(const void *anItem);
- // Return the index of the item. If the item is not in the
- // Vector, return a negative value.
-
-
- void Sort(int (*aCompareFunc)(const void *, const void *));
- // Sort the vector using the comparison function passed in.
-
- virtual void MakeEmpty();
- // Empty the Vector.
-
-
- protected:
- void *_At(int i) const;
- void _AtPut(int i, const void *anItem);
- // Unsafe versions, allowing derived classes to sidestep
- // bounds checks when they have proved the indices are valid
-
- private:
- int fLength;
- };
-
-
- //µ Vector::IVector
- #pragma segment Vector
- inline int Vector::IVector()
- {
- return (IVector(0, 4));
- }
-
-
- //µ Vector::SetIncrement
- #pragma segment Vector
- inline void Vector::SetIncrement(size_t anIncrement)
- {
- // Set the number of items that the vector is expanded by whenever
- // it must be expanded.
- DataArea::SetIncrement(anIncrement * sizeof(void *));
- }
-
-
- //µ Vector::Length
- #pragma segment Vector
- inline int Vector::Length() const
- {
- return (fLength);
- }
-
-
- //µ Vector::_Item
- #pragma segment Vector
- inline void *Vector::_At(int i) const
- {
- return ((*(void ***)DataArea::fHandle)[i]);
- }
-
-
- //µ Vector::Item
- #pragma segment Vector
- inline void *Vector::At(int i) const
- {
- return ((i >= 0 && i < fLength) ? _At(i) : 0);
- }
-
-
- //µ Vector::_SetItem
- #pragma segment Vector
- inline void Vector::_AtPut(int i, const void *anItem)
- {
- (*(void ***)DataArea::fHandle)[i] = anItem;
- }
-
-
-
-
- #endif
-
-
-